package com.isias.system.Controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isias.common.result.Result;
import com.isias.model.El.ElPaper;
import com.isias.model.El.ElQu;
import com.isias.model.El.ElRepo;
import com.isias.model.vo.ElPaperQueryVo;
import com.isias.model.vo.ElQuQueryVo;
import com.isias.model.vo.ElRepoQueryVo;
import com.isias.system.Service.IAuthenticationFacade;
import com.isias.system.service.ElPaperService;
import com.isias.system.service.ElRepoService;
import com.isias.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="/admin/el/paper")
public class ElPaperController {
    @Autowired
    private ElPaperService elPaperService;


    /**
     * 分页查询
     */
    Page<ElPaper> pageparam = new Page<>();
    @GetMapping("{page}/{limit}")
    public Result SeletePage(@PathVariable Integer page, @PathVariable Integer limit, ElPaperQueryVo elPaperQueryVo){
        pageparam.setCurrent(page);
        pageparam.setSize(limit);
        IPage<ElPaper> iPage = elPaperService.SelectPage(pageparam,elPaperQueryVo);
        return Result.ok(iPage);

    }

    /***
     * 根据试卷id查询到试卷包含的题目Qu
     * @param paperid
     * @return
     */
    @GetMapping("/PaperQu/{paperid}")
    public Result SelectQuByPaperId(@PathVariable String paperid, ElQuQueryVo elQuQueryVo){
        List<ElQu> PaperQu = elPaperService.SelectQuByPaperId(paperid,elQuQueryVo);
        return Result.ok(PaperQu);

    }


    /**
     * 查询单个（用于数据回显）
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result GetById(@PathVariable String id){
        ElPaper elPaper = elPaperService.getById(id);
        if (elPaper != null){
            return Result.ok(elPaper);
        }
        return Result.fail(elPaper);
    }

    /**
     * 添加试卷
     * @param elPaper
     * @return
     */
    @PostMapping
    public Result Add(@RequestBody ElPaper elPaper){

        if (elPaperService.save(elPaper)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改单个
     * @param elPaper
     * @return
     */
    @PutMapping()
    public Result Update(@RequestBody ElPaper elPaper){
        if (elPaperService.updateById(elPaper)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result DeleteById(@PathVariable String id){
        if (elPaperService.removeById(id)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id列表删除
     */
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<String> idList) {
        if (elPaperService.removeByIds(idList)) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /***
     * 刷新试卷的信息(目的：重新计算有多少道单选、多选、判断、论述题，并更新到数据库中)
     * @return
     */
    @GetMapping("/refresh/{id}")
    public Result reFresh(@PathVariable String id){
        elPaperService.reFresh(id);
        return Result.ok();
    }

    /***
     * 把某个题目添加到试卷上
     * @param paperId
     * @param quId
     * @return
     */
    @GetMapping("/addQuToPaper/{paperId}/{quId}")
    public Result AddQuToRepo(@PathVariable String paperId,@PathVariable String quId){
        if (elPaperService.AddQuToRepo(paperId,quId)){
            return Result.ok();
        }
        return Result.fail("题库中已存在或系统异常");
    }


    /***
     * 试卷上中删除某题
     * @param paperId
     * @param quId
     * @return
     */
    @DeleteMapping("/removeQuToPaper/{paperId}/{quId}")
    public Result removeQuToPaper(@PathVariable String paperId,@PathVariable String quId){
        if (elPaperService.removeQuToPaper(paperId,quId)){
            return Result.ok();
        }
        return Result.fail();
    }





}
